package com.systop.front.client.controller;
import com.systop.busafter.bus.service.BusService;
import com.systop.busafter.comment.service.CommentService;
import com.systop.busafter.driver.service.DriverService;
import com.systop.busafter.location.service.LocationService;
import com.systop.busafter.task.service.TaskService;
import com.systop.busafter.user.service.UserService;
import com.systop.commer.ResponseResult;
import com.systop.commer.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class ClientController {
    @Autowired
    private DriverService driverService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private BusService busService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;

    //后台车表，通过司机账号查看，
    @RequestMapping(value="/busdrivernamenlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult drivername(@RequestParam(value = "driver_name_n")String driver_name_n){
       Driver driver = driverService.drivername1(driver_name_n);
        if (driver == null){
            return ResponseResult.build(20, "请检查您输入的账号");
        }else{
            List<Bus>  list1 = busService.driverIdall(driver.getDriver_id());
            if (list1 == null) {
                return ResponseResult.build(500, "账号查失败");
            } else {
                return ResponseResult.build(200, "账号查成功", list1);
            }
        }
    }
    //后台评论表，通过司机账号查看，评论表信息
    @RequestMapping(value="/commentdrivernamenlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult commentdrivernamenlist(@RequestParam(value = "driver_name_n")String driver_name_n){
        Driver driver1 = driverService.drivername1(driver_name_n);
        if (driver1 == null){
            return ResponseResult.build(20, "请检查您输入的账号");
        }else{
            List<Comment> list1 = commentService.commentdriveridall(driver1.getDriver_id());
            if (list1 == null) {
                return ResponseResult.build(500, "账号查失败");
            } else {
                return ResponseResult.build(200, "账号查成功", list1);
            }
        }
    }
    //后台评论表，通过用户账号查看，评论表信息
    @RequestMapping(value="/commentusernamenlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult commentusernamenlist(@RequestParam(value = "user_name_n")String user_name_n){
        User list = userService.userZhlist1(user_name_n);
        if (list == null){
            return ResponseResult.build(20, "请检查您输入的账号");
        }else{
            List<Comment> list1 = commentService.commentuseridall(list.getUser_id());
            if (list1 == null) {
                return ResponseResult.build(500, "账号查失败");
            } else {
                return ResponseResult.build(200, "账号查成功", list1);
            }
        }
    }
    //修改task的押金状态，用户提交押金
    @RequestMapping(value="/taskdepositstateTure",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult taskdepositstateTure(@RequestParam(value = "task_id")String task_id){
        int ii= Integer.parseInt(task_id);
        Task task = taskService.TaskId(ii);
        task.setTask_deposit_state(1);
        int i = taskService.updatatask(task);
        if (i == 0){
                return ResponseResult.build(500, "修改状态失败");
            } else {
                return ResponseResult.build(200, "修改状态成功", task);
            }
     }


    //修改task的押金状态，用户提交押金
    @RequestMapping(value="/Tuidan",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult Tuidan(@RequestParam(value = "task_id")String task_id) throws ParseException {
        Task task = taskService.TaskId(Integer.parseInt(task_id));

        Date date = task.getTask_time();
        // 获得SimpleDateFormat类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制
        //上次的时间，当前时间
        String a = simpleDateFormat.format(date);
        String b = simpleDateFormat.format(new Date());
        long time = simpleDateFormat.parse(a).getTime();
        long time1 = simpleDateFormat.parse(b).getTime();
        if((time1-time)>3600000) {
            return ResponseResult.build(500, "超过一小时",task);
        } else{
                return ResponseResult.build(200, "未超过一小时", task);
            }
        }


    //修改task的订单状态，用户退款
    @RequestMapping(value="/Tuidan1",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult Tuidan1(@RequestParam(value = "task_id")String task_id) throws ParseException {
        Task task = taskService.TaskId(Integer.parseInt(task_id));
        task.setTask_state(1);
        int i= taskService.updatatask(task);
        if(i==0) {
            return ResponseResult.build(500, "修改状态成功",task);
        } else{
            return ResponseResult.build(200, "修改状态失败", task);
        }
    }

}


